<?php

namespace App\Http\Controllers\Article;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Validator, Redirect, Session, Captcha;
use App\Http\Model\Article\Section;
use App\Http\Model\Article\Tags;
use App\Http\Model\Article\Article;
use App\Http\Model\Article\ArticleTages;

class  ArticleController extends Controller{
    public  function  add_index(Request $request){
       if(!Session::has('user_info')){
           return view('user.user.home');
       }
        $tags = Tags::paginate(15);
       return view('user.jie.add')->with('tags',$tags);
    }
    public  function  add_submit(Request $request){
        $input=Input::all();

         $user_info=Session::get('user_info');

        $Article = new Article();
        $Article->article_name = $input['article_name'];
        $Article->article_content = $input['article_content'];
        $Article->section_id = $input['section_id'];
        $Article->article_author = $user_info['user_name'];
        $Article->article_author_id = $user_info['user_id'];
        $Article->created_at = date('Y-m-d H:i:s');
        $Article->updated_at = date('Y-m-d H:i:s');
        $Article->save();
        $Article_id = $Article->id;


        $ArticleTages=new ArticleTages();
        $ArticleTages->article_id=$Article_id;
        $ArticleTages->article_title=$input['article_name'];
        $ArticleTages->article_author=$user_info['user_name'];
        $ArticleTages->created_at = date('Y-m-d H:i:s');
        $ArticleTages->updated_at = date('Y-m-d H:i:s');
        $ArticleTages->tags_id=$input['tags_id'];
        $ArticleTages->tags_name=$input['tags_name'];
        $ArticleTages->save();

        Tags::where('tags_id', $input['tags_id'])->increment("tags_article_num");//自1



        return redirect('article/details/'.$Article_id);
    }
    public  function  details($id,Request $request){
        ArticleTages::where('article_id',  $id)->increment("article_hits");//自1
        Article::where('article_id',  $id)->increment("article_hits");//自1
        $details = Article::where('article_id', $id)->first();
        return view('user.jie.detail')->with('details',$details);
    }
    public  function search(Request $request){
        $input=Input::all();
        $keywords=$input['article_title'];
        $article_list=Article::where('article_name','like','%'.$keywords.'%')->orderBy('updated_at','asc')->paginate(50);
        $tags_list = Tags::orderBy('tags_article_num','asc')->paginate(50);
        $hot_hits_article_list = Article::orderBy('article_hits','asc')->paginate();
        return view('welcome', ['article_list' => $article_list,'hot_hits_article_list'=>$hot_hits_article_list,'tags_list'=>$tags_list]);
    }
}